package net.tv90.appserveradmin.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import net.tv90.appserveradmin.model.Setmeal;
import net.tv90.appserveradmin.model.SetmealDetailed;
import net.tv90.appserveradmin.model.dto.SetmealAddDTO;
import net.tv90.appserveradmin.model.dto.SetmealDTO;
import net.tv90.appserveradmin.model.vo.SetmealVO;
import net.tv90.appserveradmin.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/setmeals")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @SaCheckRole("doctor")
    @GetMapping
    public String listSetmeals(Model model) {
        List<SetmealVO> setmeals = setmealService.listAllSetmealsWithCheckitemNames();
        model.addAttribute("setmeals", setmeals);
        return "setmeals/list";
    }

    @SaCheckRole("doctor")
    @GetMapping("/view/{id}")
    public String viewSetmeals(@PathVariable Long id, Model model) {
        SetmealDTO setmeals = setmealService.viewSetmeals(id);
        model.addAttribute("setmeals", setmeals);
        return "setmeals/view";
    }

    @SaCheckRole("doctor")
    @GetMapping("/add")
    public String showAddForm(Model model) {
        model.addAttribute("setmeal", new SetmealAddDTO());
        model.addAttribute("checkitems", setmealService.getAllCheckitems()); // 获取所有检查项
        return "setmeals/add";
    }

    @SaCheckRole("doctor")
    @PostMapping("/add")
    public String addSetmeal(@ModelAttribute SetmealAddDTO setmealAddDTO) {
        setmealService.addSetmeal(setmealAddDTO);
        return "redirect:/setmeals";
    }

    @SaCheckRole("doctor")
    @GetMapping("/edit/{smId}")
    public String showEditForm(@PathVariable Integer smId, Model model) {
        Setmeal setmeal = setmealService.getSetmealById(smId);
        SetmealAddDTO setmealAddDTO = new SetmealAddDTO();
        setmealAddDTO.setSmId(setmeal.getSmId());
        setmealAddDTO.setName(setmeal.getName());
        setmealAddDTO.setType(setmeal.getType());
        setmealAddDTO.setPrice(setmeal.getPrice());
        List<SetmealDetailed> details = setmealService.getSetmealDetails(smId);
        List<Integer> checkitemIds = details.stream()
                .map(SetmealDetailed::getCiId)
                .collect(Collectors.toList());
        setmealAddDTO.setCheckitemIds(checkitemIds);
        model.addAttribute("setmeal", setmealAddDTO);
        model.addAttribute("checkitems", setmealService.getAllCheckitems()); // 获取所有检查项
        return "setmeals/edit";
    }

    @SaCheckRole("doctor")
    @PostMapping("/edit")
    public String editSetmeal(@ModelAttribute("setmeal") SetmealAddDTO setmealAddDTO) {
        setmealService.updateSetmeal(setmealAddDTO);
        return "redirect:/setmeals";
    }

    @SaCheckRole("doctor")
    @GetMapping("/delete/{smId}")
    public String deleteSetmeal(@PathVariable Integer smId) {
        setmealService.deleteSetmeal(smId);
        return "redirect:/setmeals";
    }
}
